home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / pgraph.zip / C.ZIP / DEMO.C < prev    next >
Text File  |  1991-07-11  |  9KB  |  302 lines

  1. /********************************************************************
  2.  *                                                                  *
  3.  *  "Printer Graphics Interface" Demonstration Program              *
  4.  *                                                                  *
  5.  *  This program demonstrates how to use various functions          *
  6.  *  available in the PGRAPH library.                                *
  7.  *                                                                  *
  8.  *  Author: F van der Hulst                                         *
  9.  *                                                                  *
  10.  * Revisions:                                                       *
  11.  * 27 March 1991: Initial release (Turbo C v2.0 only)               *
  12.  * 07 April 1991: Ported to MicroSoft C v5.1                        *
  13.  *                                                                  *
  14.  ********************************************************************/
  15.  
  16. #include <io.h>
  17. #include <fcntl.h>
  18. #include <sys\stat.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <conio.h>
  23. #ifdef __TURBOC__
  24. #include <alloc.h>
  25. #else
  26. #include <malloc.h>
  27. #endif
  28.  
  29. #if defined(__HUGE__) || defined(__LARGE__) || defined(__MEDIUM__)
  30. #define LARGE_CODE
  31. #endif
  32.  
  33. #ifdef LARGE_CODE
  34. #ifdef __TURBOC__
  35. #include <graphics.h>
  36. #include <bgidrive.h>
  37. #else
  38. #include <graph.h>
  39. #endif
  40. #endif
  41.  
  42. #include "pgraph.h"
  43.  
  44. void shapes_demo(void);
  45. void stroked_fonts_demo(void);
  46. void default_font_demo(void);
  47. void horiz_text_demo(void);
  48. void vert_text_demo(void);
  49. void text_scaling_demo(void);
  50. void shape_fill_demo(void);
  51. void flood_fill_demo(void);
  52. void lines_demo(void);
  53. void pie_demo(void);
  54.  
  55. #if defined(LARGE_CODE)
  56. void image_demo(void);
  57. void view_demo(void);
  58. #endif
  59.  
  60. void LASERJET_DRIVER(void);            /* PGI Object entries */
  61. extern int far _Cdecl STAR_DRIVER[];
  62.  
  63. #define MAX_WIDTH 801            /* Maximum width of any PGRAPH viewport defined in the program */
  64.  
  65. int screen_echo = 0;                /* Echo printer output to screen or not */
  66. int page_height, page_width;    /* Size of page in pixels */
  67. int prn;                                /* Output device */
  68.  
  69. struct {                                /* Page sizes in 1/100 of an inch available on various printers */
  70.     int x;
  71.     int y;
  72. } page_size[] = {
  73.     {0, 0},
  74.     {800, 1100},                    /* STAR */
  75.     {780, 1088},               /* LaserJet */
  76.     {800, 1100},                    /* Epson LX400 */
  77.     {800, 1100},                    /* USER1 */
  78.     {800, 1100} };                    /* USER2 */
  79.  
  80.  
  81. #if defined(LARGE_CODE)
  82. #define my_farmalloc (void far *) farmalloc
  83. #define my_farfree   (void far *) farfree
  84. #else
  85. /* The following two routines are required in any 64K Code model, since
  86.     _p_graphgetmem and _p_graphfreemem MUST point to a HUGE function, and
  87.     in small code model libraries, these functions are NEAR */
  88.  
  89. void huge my_farfree(void far *ptr, unsigned long size)
  90. {
  91.     farfree(ptr);
  92. #pragma warn -par
  93. }
  94. #pragma warn .par
  95.  
  96. void far * huge my_farmalloc(unsigned long size)
  97. {
  98.     return farmalloc(size);
  99. }
  100. #endif
  101.  
  102. /********************************************************************
  103.  Process command line arguments. */
  104.  
  105. void get_args(int argc, char *argv[], int *driver, int *mode, char *dev_name, char *demos)
  106. {
  107. int i, low, high;
  108.  
  109.     for (i = 1; i < argc; i++) {
  110.         strupr(argv[i]);
  111.         if (argv[i][0] != '/' && argv[i][0] != '-') {
  112.             printf("Invalid command line switch: %s\n(Must start with '-' or '/')\n", argv[i]);
  113.             printf("Use /? to get help\n");
  114.             exit(1);
  115.         }
  116.         if (argv[i][1] == '?' || argv[i][1] == 'H') {
  117.             printf("Command syntax: %s [/O=outputdevice][/P=printer][/M=mode][/D=demos]\n\n", argv[0]);
  118.             printf("outputdevice may be PRN, or a filename\n");
  119.             printf("printer may be STAR, LASERJET, LX-400, USER1, or USER2\n");
  120.             printf("\tIf you use LX-400, USER1, USER2, the corresponding .PGI file\n\tmust be in the current directory\n");
  121.             printf("mode is an integer in the range 0 to the maximum mode for the selected printer\n");
  122.             printf("demos is a series of letters (A-L), identifying which demos to print\n");
  123.             printf("\nDefault values are PRN and STAR, and a mode better than 120dpi)\n\n");
  124.             exit(0);
  125.         }
  126.         if (argv[i][2] != '=') {
  127.             printf("Invalid command line switch: %s\n(Must be /%c=VALUE)\n", argv[i], argv[i][1]);
  128.             printf("Use /? to get help\n");
  129.             exit(1);
  130.         }
  131.         switch(argv[i][1]) {
  132.         case 'O':
  133.         case 'o':
  134.             strcpy(dev_name, &argv[i][3]);
  135.             break;
  136.         case 'P':
  137.         case 'p':
  138.             if       (strcmp(&argv[i][3], "STAR")         == 0)    *driver = STAR;
  139.             else if (strcmp(&argv[i][3], "LX-400")     == 0)    *driver = LX400;
  140.             else if (strcmp(&argv[i][3], "LASERJET")    == 0) *driver = LASERJET;
  141.             else if (strcmp(&argv[i][3], "USER1")         == 0)    *driver = USER1;
  142.             else if (strcmp(&argv[i][3], "USER2")         == 0)    *driver = USER2;
  143.             else {
  144.                 printf("Unknown printer type: %s\n", &argv[i][3]);
  145.                 printf("Use /? to get help\n");
  146.                 exit(1);
  147.             }
  148.             break;
  149.         case 'M':
  150.         case 'm':
  151.             *mode = atoi(&argv[i][3]);
  152.             p_getmoderange(*driver, &low, &high);
  153.             if (*mode > high || *mode < low) {
  154.                 printf("Invalid mode: %d (should be %d - %d\n", *mode, low, high);
  155.                 exit(1);
  156.             }
  157.             break;
  158.         case 'D':
  159.         case 'd':
  160.             strcpy(demos, &argv[i][3]);
  161.             break;
  162.         default:
  163.             printf("Invalid command line switch: %s\n(Must be /D, /O, /P, or /M)\n", argv[i]);
  164.             printf("Use /? to get help\n");
  165.             exit(1);
  166.         }
  167.     }
  168. }
  169.  
  170. /********************************************************************
  171.  Find the best mode (the worst X resolution that will display
  172.  MAX_WIDTH bits) for the selected printer. */
  173.  
  174. int best_mode(void)
  175. {
  176. int i;
  177. int xres, yres, best_x, best_y;
  178. int mode;
  179.  
  180.     mode = 0;
  181.     best_y = best_x = 1000;
  182.     for (i = 0; i <= p_getmaxmode(); i++) {
  183.         p_setgraphmode(i);
  184.         p_getresolution(&xres, &yres);
  185.         if ((long)xres * page_width / 100 >= MAX_WIDTH+7)
  186.             if ((xres < best_x) || (xres == best_x && yres < best_y)) {
  187.                 best_y = yres;
  188.                 best_x = xres;
  189.                 mode = i;
  190.             }
  191.     }
  192.     return mode;
  193. }
  194.  
  195. void main(int argc, char *argv[])
  196. {
  197. int driver = 1, mode = -1, dummy_mode, errorcode;
  198. int xres, yres;
  199. static char filename[80] = "PRN";
  200. static char selection[] = "ABCDEFGHIJKL";
  201. static char *printer_ID[] = { "", "STAR", "LASERJET", "LX-400", "USER1", "USER2" };
  202.  
  203.     _p_graphgetmem      = my_farmalloc;
  204.     _p_graphfreemem      = my_farfree;
  205.     _p_putpixel_screen = NULL;
  206.  
  207.     if (p_registerbgidriver(LASERJET_DRIVER) < 0) {
  208.         printf("Couldn't register LASERJET PGI driver\n");
  209.         return;
  210.     }
  211.     if (p_registerfarbgidriver(STAR_DRIVER) < 0) {
  212.         printf("Couldn't register STAR PGI driver\n");
  213.         return;
  214.     }
  215.  
  216.     get_args(argc, argv, &driver, &mode, filename, selection);
  217.  
  218.     printf("Selection is:\n\tOutput to %s\n", filename);
  219.     printf("\tPrinter type is %s\n", printer_ID[driver]);
  220.     printf("Demo selection is %s\n\n", selection);
  221.     printf("Is this OK (Y/N)? ");
  222.     if ((getch() & 0x0df) != 'Y') exit(1);
  223.     printf("\n\n");
  224.     if (strcmp(filename, "PRN") != 0) {
  225.         prn = open(filename,
  226.                       O_WRONLY | O_BINARY | O_CREAT | O_TRUNC,
  227.                       S_IREAD | S_IWRITE);
  228.         if (prn == -1) {
  229.             printf("Couldn't open %s for output\n", filename);
  230.             exit(1);
  231.         }
  232.     } else prn = fileno(stdprn);
  233.  
  234.     dummy_mode = mode == -1 ? 0 : mode;
  235.  
  236.     p_initgraph(&driver, &dummy_mode, NULL);
  237.     errorcode = p_graphresult();             /* preserve error return */
  238.     if (errorcode != grOk) {                    /* error? */
  239. #if defined(__TURBOC__) && defined(LARGE_CODE)
  240.         printf("Graphics error: %s\n", grapherrormsg(errorcode));
  241. #else
  242.         printf("Graphics error: %d\n", errorcode);
  243. #endif
  244.         exit(1);
  245.     }
  246.     page_height = page_size[driver].y;
  247.     page_width = page_size[driver].x;
  248.     if (mode < 0 || mode > p_getmaxmode())        mode = best_mode();
  249.     p_setgraphmode(mode);
  250.     p_getresolution(&xres, &yres);
  251.     printf("Currently set to mode %d (%d by %ddpi).\n", mode, xres, yres);
  252.     if (strchr(selection, 'A') != NULL) shapes_demo();
  253.     if (strchr(selection, 'B') != NULL) stroked_fonts_demo();
  254.     if (strchr(selection, 'C') != NULL) default_font_demo();
  255.     if (strchr(selection, 'D') != NULL) horiz_text_demo();
  256.     if (strchr(selection, 'E') != NULL) vert_text_demo();
  257.     if (strchr(selection, 'F') != NULL) text_scaling_demo();
  258.     if (strchr(selection, 'G') != NULL) shape_fill_demo();
  259.     if (strchr(selection, 'H') != NULL) flood_fill_demo();
  260.     if (strchr(selection, 'I') != NULL) lines_demo();
  261.     if (strchr(selection, 'J') != NULL) pie_demo();
  262. #if defined(LARGE_CODE)
  263.     if (strchr(selection, 'K') != NULL) image_demo();
  264.     if (strchr(selection, 'L') != NULL) view_demo();
  265. #else
  266.     if (strchr(selection, 'K') != NULL ||
  267.          strchr(selection, 'L') != NULL)
  268.         printf("Can't do K or L demos in Small Code model.\n");
  269. #endif
  270.  
  271.     printf("Closing PGRAPH down.\n");
  272.     p_closegraph();
  273.     printf("Closing output file.\n");
  274.     if (strcmp(filename, "PRN") != 0)    close(prn);
  275. }
  276.  
  277. /********************************************************************
  278.  End of outputting a slice to the buffer. Check to see whether it will
  279.  fit on the current page. If not skip to the top of the next page */
  280.  
  281. void end_slice(void)
  282. {
  283. int xres, yres;
  284. int height;
  285. static int line_num = 0;
  286. static char FF = 0x0c;
  287.  
  288.     p_getresolution(&xres, &yres);
  289.     height = p_getmaxy() + 1;
  290.     line_num += height;
  291.     if (line_num > (long)yres * page_height / 100) {
  292.         line_num = height;
  293.         write(prn, &FF, 1);
  294.     }
  295.  
  296.     if (!screen_echo)    printf("Printing...");
  297.     p_print(prn);
  298.     if (!screen_echo) printf("\n");
  299.     p_cleardevice();
  300. }
  301.  
  302.